import tkinter as tk
import tkinter.messagebox

question_list = []
answer_list = []
true_answer_list = []
n = 0
numb = 0
jurisdiction_numb = 1


def get_file():
    global question_list
    global answer_list
    global true_answer_list
    global jurisdiction_numb
    global numb
    f = open('data.txt', 'r', encoding='utf-8')
    ft = f.read()
    f.close()
    base_list = ft.split('%')
    for i in base_list:
        temp_answer_list = []
        if i == '':
            break
        temp_list = i.split('$')
        question_list.append(temp_list[0])
        temp_answer_list.append(temp_list[1])
        for j in temp_answer_list:
            temps_answer_list = []
            temp_lists = j.split('*')
            temps_answer_list.append(temp_lists[0])
            true_answer_list.append(temp_lists[1])
            for k in temps_answer_list:
                answer_list.append(k.split('#'))
    if jurisdiction_numb == 1:
        numb = len(question_list)
        jurisdiction_numb = 0


class window():
    def __init__(self, master):
        self.root = master
        self.root.config()
        self.root.title('Answering machine')
        self.root.geometry('300x350')

        face(self.root)

class face():
    def __init__(self, master):
        self.master = master
        self.faces = tk.Frame(self.master)
        self.faces.pack()
        btn = tk.Button(self.faces, text='开始答题', comman=self.gtanswer)
        btn.pack(pady=10, ipady=10, ipadx=50)
        btn1 = tk.Button(self.faces, text='查看题目', comman=self.gtlook)
        btn1.pack(pady=10, ipady=10, ipadx=50)
        btn2 = tk.Button(self.faces, text='录入题目', comman=self.gtinput)
        btn2.pack(pady=10, ipady=10, ipadx=50)
        btn3 = tk.Button(self.faces, text='修改题目', comman=self.gtmodify)
        btn3.pack(pady=10, ipady=10, ipadx=50)

    def gtanswer(self):
        self.faces.destroy()
        answer(self.master)

    def gtlook(self):
        self.faces.destroy()
        look(self.master)

    def gtinput(self):
        self.faces.destroy()
        input(self.master)

    def gtmodify(self):
        pass


class answer():
    def __init__(self, master):
        self.master = master
        self.facesa = tk.Frame(self.master)
        self.facesa.pack()
        get_file()
        def choose():
            global n
            global jurisdiction_numb
            global numb
            user_choose = var.get()
            if user_choose == true_answer_list[n]:
                message = tkinter.messagebox.askquestion(title='系统消息', message='回答正确，继续吗？')
                if message == 'yes':
                    n += 1
                    if n == numb:
                        tkinter.messagebox.showerror(title='系统消息',  message='没有更多题目了！')
                        self.facesa.destroy()
                        face(self.master)
                        n = 0
                        jurisdiction_numb = 1
                        return 0
                    else:
                        n -= 1
                    n += 1
                    self.facesa.destroy()
                    answer(self.master)
                else:
                    self.facesa.destroy()
                    face(self.master)
                    n = 0
            else:
                messages = tkinter.messagebox.askquestion(title='系统消息', message='回答错误，是否继续')
                if messages == 'yes':
                    n += 1
                    if n == numb:
                        tkinter.messagebox.showerror(title='系统消息',  message='没有更多题目了！')
                        self.facesa.destroy()
                        face(self.master)
                        n = 0
                        jurisdiction_numb = 1
                        return  0
                    else:
                        n -= 1
                    n += 1
                    self.facesa.destroy()
                    answer(self.master)

                else:
                    self.facesa.destroy()
                    face(self.master)
                    n = 0
        def answer_face():
            global n
            ano_answer_list = answer_list[n]
            question_lable = tk.Label(self.facesa, font=('Arial', 12), bg='#c4cbcf', text=f'{n+1}、{question_list[n]}')
            radioa = tk.Radiobutton(self.facesa, text=f'A、{ano_answer_list[0]}', variable=var, value='A')
            radiob = tk.Radiobutton(self.facesa, text=f'B、{ano_answer_list[1]}', variable=var, value='B')
            radioc = tk.Radiobutton(self.facesa, text=f'C、{ano_answer_list[2]}', variable=var, value='C')
            radiod = tk.Radiobutton(self.facesa, text=f'D、{ano_answer_list[3]}', variable=var, value='D')
            btn = tk.Button(self.facesa, text='提交', command=choose)
            def exit1():
                self.facesa.destroy()
                face(self.master)
            btn_exit = tk.Button(self.facesa, text='返回', command=exit1)
            question_lable.pack()
            radioa.pack()
            radiob.pack()
            radioc.pack()
            radiod.pack()
            btn.pack()
            btn_exit.pack(side='right', pady='70')
        var = tk.StringVar()
        answer_face()




class look():
    def __init__(self, master):
        get_file()
        self.master = master
        self.faces = tk.Frame(self.master)
        self.faces.pack()

        def next_question_function():
            global n
            if n+1 == len(question_list):
                tkinter.messagebox.showerror(title='系统消息', message='没有更多题目了！')
                return 0
            n += 1
            self.facesa.destroy()
            btn_look_all_question_function()

        def btn_search_function():
            search_number_judge = int(search_input.get())
            list_long = len(question_list)
            if search_number_judge > list_long:
                tkinter.messagebox.showerror(title='系统消息', message=f'没有这么多题目！共有{list_long}道题')
                return 0
            search_data = int(search_input.get()) - 1
            self.faces.destroy()
            self.facesa = tk.Frame(self.master)
            self.facesa.pack()
            search_result_answer_list = answer_list[search_data]

            def exit2():
                self.facesa.destroy()
                look(self.master)

            question_lable = tk.Label(self.facesa, font=('Arial', 12), bg='#c4cbcf', text=f'{search_data+1}、{question_list[search_data]}')
            radioa = tk.Radiobutton(self.facesa, text=f'A、{search_result_answer_list[0]}', value='A')
            radiob = tk.Radiobutton(self.facesa, text=f'B、{search_result_answer_list[1]}', value='B')
            radioc = tk.Radiobutton(self.facesa, text=f'C、{search_result_answer_list[2]}', value='C')
            radiod = tk.Radiobutton(self.facesa, text=f'D、{search_result_answer_list[3]}', value='D')
            true_answer_lable = tk.Label(self.facesa, font=('Arial', 12), bg='#ffa60f', text=f'正确答案：{true_answer_list[search_data]}')
            btn_exit = tk.Button(self.facesa, text='返回', command=exit2)
            question_lable.pack()
            radioa.pack()
            radiob.pack()
            radioc.pack()
            radiod.pack()
            true_answer_lable.pack()
            btn_exit.pack(side='right')

        def btn_look_all_question_function():
            self.faces.destroy()
            self.facesa = tk.Frame(self.master)
            self.facesa.pack()

            def exit5():
                global n
                global question_list
                global answer_list
                n = 0
                self.facesa.destroy()
                face(self.master)
                question_list = []
                answer_list = []

            def exit4():
                global n
                if n > 0:
                    n -= 1
                else:
                    tkinter.messagebox.showerror(title='系统消息', message='没有上一题了！')
                    return 0
                self.facesa.destroy()
                btn_look_all_question_function()

            temp_answer_list = answer_list[n]
            question_lable = tk.Label(self.facesa, font=('Arial', 12), bg='#c4cbcf', text=f'{n+1}、{question_list[n]}')
            radioa = tk.Radiobutton(self.facesa, text=f'A、{temp_answer_list[0]}', value='A')
            radiob = tk.Radiobutton(self.facesa, text=f'B、{temp_answer_list[1]}', value='B')
            radioc = tk.Radiobutton(self.facesa, text=f'C、{temp_answer_list[2]}', value='C')
            radiod = tk.Radiobutton(self.facesa, text=f'D、{temp_answer_list[3]}', value='D')
            true_answer_lable = tk.Label(self.facesa, font=('Arial', 12), bg='#ffa60f', text=f'正确答案：{true_answer_list[n]}')
            button_next_question = tk.Button(self.facesa, text='下一题', command=next_question_function)
            btn_exit = tk.Button(self.facesa, text='上一题', command=exit4)
            btn_exit1 = tk.Button(self.facesa, text='返回主界面', command=exit5)
            question_lable.pack()
            radioa.pack()
            radiob.pack()
            radioc.pack()
            radiod.pack()
            true_answer_lable.pack()
            button_next_question.pack()
            btn_exit.pack(side='right', ipadx='2px')
            btn_exit1.pack(side='right')

        search_input = tk.Entry(self.faces, font=('Arial', 12))
        btn_search = tk.Button(self.faces, text='查询（输入题号）', command=btn_search_function)
        btn_look_all_question = tk.Button(self.faces, text='查看所有题目', command=btn_look_all_question_function)

        def exit3():
            self.faces.destroy()
            face(self.master)

        btn_exit = tk.Button(self.faces, text='返回', command=exit3)
        search_input.pack()
        btn_search.pack()
        btn_look_all_question.pack(pady='20', ipadx='15', ipady='5')
        btn_exit.pack(side='right')

class input():
    def __init__(self, master):
        self.master = master
        self.faces =tk.Frame(self.master)
        self.faces.pack()
        inputtext = tk.Text(self.faces, width=30, height=3, font=('Arial', 12))
        inputtext.pack()
        lablea = tk.Label(self.faces, font=('Arial', 12), text='A')
        ea = tk.Entry(self.faces, font=('Arial', 12))
        lableb = tk.Label(self.faces, font=('Arial', 12), text='B')
        eb = tk.Entry(self.faces, font=('Arial', 12))
        lablec = tk.Label(self.faces, font=('Arial', 12), text='C')
        ec = tk.Entry(self.faces, font=('Arial', 12))
        labled = tk.Label(self.faces, font=('Arial', 12), text='D')
        ed = tk.Entry(self.faces, font=('Arial', 12))
        lablet = tk.Label(self.faces, font=('Arial', 12), text='正确答案')
        et = tk.Entry(self.faces, font=('Arial', 12))
        lablea.pack(fill='x')
        ea.pack()
        lableb.pack(fill='x')
        eb.pack()
        lablec.pack(fill='x')
        ec.pack()
        labled.pack(fill='x')
        ed.pack()
        lablet.pack(fill='x')
        et.pack()
        def submit():
             var = inputtext.get('0.0', 'end')
             vara = ea.get()
             varb = eb.get()
             varc = ec.get()
             vard = ed.get()
             vart = et.get()
             if var and vara and varb and varc and vard and vart != '':
                 messages = tkinter.messagebox.askquestion(title='确定', message=f'问题：{var}A:{vara}  B:{varb}  C:{varc}  D:{vard}\n正确答案:{vart}\n确定录入？')
                 if messages == 'yes':
                     f = open('data.txt', mode='a+', encoding='utf-8')
                     f.write(var+'$'+vara+'#'+varb+'#'+varc+'#'+vard+'*'+vart+'%')
                     f.close()
                     messagess = tkinter.messagebox.askquestion(title='系统消息', message='录入成功！是否继续录入？')
                     if messagess == 'yes':
                         self.faces.destroy()
                         input(self.master)
                     else:
                         self.faces.destroy()
                         face(self.master)
                 else:
                     pass
             else:
                 message = tkinter.messagebox.askquestion(title='系统消息', message='题目或答案不能为空！是否修改？点否返回主界面')
                 if message == 'yes':
                     pass
                 else:
                     self.faces.destroy()
                     face(self.master)


        btn = tk.Button(self.faces, text='提交', command=submit)
        btn.pack(ipadx='50')
        def exit():
            self.faces.destroy()
            face(self.master)
        btn_exit = tk.Button(self.faces, text='返回', command=exit)
        btn_exit.pack(side='right')


class modify():
    def __init__(self, master):
        self.master = master


if __name__  ==  '__main__':
    #创建实例
    root = tk.Tk()
    #传入实例参数
    window(root)
    #循环执行
    root.mainloop()

